package com.zhwmall.product.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhwmall.common.entity.product.SpuImagesEntity;
import com.zhwmall.product.dao.SpuImagesDao;
import com.zhwmall.product.service.SpuImagesService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {


    @Override
    public void saveImages(Long spuId, List<String> images) {

        if (CollUtil.isNotEmpty(images)){
            List<SpuImagesEntity> imagesEntities = images.stream().map(img -> {
                SpuImagesEntity imagesEntity = new SpuImagesEntity();
                imagesEntity.setSpuId(spuId);
                imagesEntity.setImgUrl(img);
                imagesEntity.setImgName(img.substring(img.lastIndexOf("//") + 1, img.lastIndexOf(".")));
                imagesEntity.setDefaultImg(1);
//                imagesEntity.setImgSort(Integer.parseInt(IdUtil.randomUUID()));
                return imagesEntity;
            }).collect(Collectors.toList());

            this.saveBatch(imagesEntities);
        }
    }
}